package com.peza.postman.mail;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.stereotype.Service;

import com.peza.postman.domain.MailRequest;
import com.peza.postman.domain.MailResponse;

@Service("mailcenter")
public class MailCenterImpl implements MailCenter {

	 //private final static String DEFAULT_SERVER = "smtp.gmail.com";
	 private final static String DEFAULT_SERVER = "mail.pezadesign.com";
	 private final static String SMTP_AUTH_USER = "peter@lilyrock.be";
	 private final static String SMTP_AUTH_PWD = "peter";

	 public MailResponse send(MailRequest mail) {
	 
		MailResponse mr = new MailResponse();   
		 
	 	String smtpServ = DEFAULT_SERVER;
	    String from = mail.getFrom();
	    String to = mail.getTo();
	    String subject = mail.getSubject();
	    String emailContent = mail.getContent();
	    

	    try {
	      mr = sendMessage(mr, smtpServ, to, from, subject, emailContent);
	    } catch (MessagingException e) {
	    	System.err.println("woeps");
	    }
	    return mr;
	 }
	    
	 
	private MailResponse sendMessage(MailResponse mr, String smtpServer, String to, String from, String subject, String emailContent) 
			throws MessagingException {

		
		Properties properties = System.getProperties();

		// populate the 'Properties' object with the mail
		// server address, so that the default 'Session'
		// instance can use it.
		properties.put("mail.smtp.host", smtpServer);
		properties.put("mail.smtp.port", 2525);
		properties.put("mail.smtp.auth", "true");
		
		Authenticator auth = new SMTPAuthenticator();
		Session session = Session.getDefaultInstance(properties, auth);
		
		Message mailMsg = new MimeMessage(session);// a new email message

		InternetAddress[] addresses = null;

		try {

			if (to != null) {
				// throws 'AddressException' if the 'to' email address
				// violates RFC822 syntax
				addresses = InternetAddress.parse(to, false);
				mailMsg.setRecipients(Message.RecipientType.TO, addresses);
				

			} else {
				throw new MessagingException("The mail message requires a 'To' address.");
			}

			if (from != null) {
				mailMsg.setFrom(new InternetAddress(from));
			} else {
				throw new MessagingException("The mail message requires a valid 'From' address.");
			}

			if (subject != null) {
				mailMsg.setSubject(subject);
			}

			if (emailContent != null){
				mailMsg.setText(emailContent);
			}

			// Finally, send the meail message; throws a 'SendFailedException'
			// if any of the message's recipients have an invalid adress
			Transport.send(mailMsg);

		} catch (MessagingException me) {
			mr.setMessage(me.getMessage());
			throw me;
		}

		mr.setSuccess(true);
		return mr;
		
	}
	 
	 
	
	private class SMTPAuthenticator extends javax.mail.Authenticator {
        public PasswordAuthentication getPasswordAuthentication() {
           String username = SMTP_AUTH_USER;
           String password = SMTP_AUTH_PWD;
           return new PasswordAuthentication(username, password);
        }
    }
	 
}
